package servlet;

import dao.LoginDateBase;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "loginServlet")
public class loginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String username=request.getParameter("username");
       String password=request.getParameter("password");
       boolean login = false;
        LoginDateBase loginDateBase=new LoginDateBase();
        try {
            login=loginDateBase.Login(username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (login) {
                request.getSession().setAttribute("username",username);
                request.getSession().setAttribute("islogin", "true");
                request.getRequestDispatcher("/index").forward(request,response);

            } else {
                request.getSession().setAttribute("islogin", "fals");
                request.getSession().setAttribute("wrong", "用户名或密码错误！");
                request.getRequestDispatcher("/login.jsp").forward(request, response);

            }
    }
}
